package zozopo.manual.operations;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class ReplyOS extends OperatingService{
	
	public ReplyOS(Connection conn){
		super(conn);
	}
	
	public ReplyOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @param ReleaseTB release
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see insertRelease(ImageTB image)
	 */
	public boolean replySubject(ReplyTB reply){
		boolean eResult = false;
		
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		insertMap.put(ReplyTB.REPLYS_CONTEXT, reply.getReplys_context());
		//insertMap.put(ReplyTB.REPLYS_DATE, reply.getReplys_date());
		//insertMap.put(ReplyTB.REPLYS_EMAIL, reply.getReplys_email());
		insertMap.put(ReplyTB.REPLYS_IP, reply.getReplys_ip());
		insertMap.put(ReplyTB.REPLYS_KIND, reply.getReplys_kind());
		insertMap.put(ReplyTB.REPLYS_NAME, reply.getReplys_name());
		insertMap.put(ReplyTB.REPLYS_RELEASE_ID, reply.getReplys_release_id());
		//insertMap.put(ReplyTB.REPLYS_SUBJECT, reply.getReplys_subject());

		SqlMachine sqlMachine = new SqlMachine(conn);
		eResult = sqlMachine.insertRow(ReplyTB.TABLE_NAME, insertMap);
		
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @param ReleaseTB release
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see insertRelease(ImageTB image)
	 */
	public List<ReplyTB> selectReplyBySubjectId(int id,int beginPageNum,int pageSize){
		List<ReplyTB> relyList = null;
		String sql = "select * from "+ReplyTB.TABLE_NAME +
		" where " +ReplyTB.REPLYS_RELEASE_ID + " = "+id +" order by "+ReplyTB.REPLYS_DATE+" desc limit "+beginPageNum+","+pageSize; 
		
		relyList = selectReplyBySql(sql);
		
		return relyList;
	}
	
	/**
	 * @version 1.0
	 * @param String brand
	 * @author Shea
	 * @return ResultSet  
	 * @throws IOException 
	 * @see 删除主题相关回复
	 */
	public boolean deleteReplybySubjectId(int id){
		SqlMachine sqlMachine = new SqlMachine(conn);
		return sqlMachine.deleteRow(ReplyTB.TABLE_NAME, ReplyTB.REPLYS_RELEASE_ID, id);
	}
	
	/**
	 * @version 1.0
	 * @param ReleaseTB release
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see 通过sql语句得到reply例表
	 */
	public List<ReplyTB> selectReplyBySql(String sql){
		List<ReplyTB> relyList = null;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				relyList = new ArrayList<ReplyTB>();
				while(resultSet.next()){
					ReplyTB reply = new ReplyTB();
					
					reply.setReplys_id(resultSet.getInt(ReplyTB.REPLYS_ID));
					reply.setReplys_context(resultSet.getString(ReplyTB.REPLYS_CONTEXT));
					reply.setReplys_date(resultSet.getString(ReplyTB.REPLYS_DATE));
					reply.setReplys_email(resultSet.getString(ReplyTB.REPLYS_EMAIL));
					reply.setReplys_ip(resultSet.getString(ReplyTB.REPLYS_IP));
					reply.setReplys_kind(resultSet.getString(ReplyTB.REPLYS_KIND));
					reply.setReplys_name(resultSet.getString(ReplyTB.REPLYS_NAME));
					reply.setReplys_release_id(resultSet.getInt(ReplyTB.REPLYS_RELEASE_ID));
					reply.setReplys_subject(resultSet.getString(ReplyTB.REPLYS_SUBJECT));
					relyList.add(reply);
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
		
		return relyList;
	}
	
	/**
	 * @version 1.0
	 * @param ReleaseTB release
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see 通过resutlset语句得到reply例表
	 */
	public List<ReplyTB> selectReplyByResultSet(ResultSet resultSet){
		List<ReplyTB> relyList = null;
		
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				relyList = new ArrayList<ReplyTB>();
				while(resultSet.next()){
					ReplyTB reply = new ReplyTB();
					
					reply.setReplys_id(resultSet.getInt(ReplyTB.REPLYS_ID));
					reply.setReplys_context(resultSet.getString(ReplyTB.REPLYS_CONTEXT));
					reply.setReplys_date(resultSet.getString(ReplyTB.REPLYS_DATE));
					reply.setReplys_email(resultSet.getString(ReplyTB.REPLYS_EMAIL));
					reply.setReplys_ip(resultSet.getString(ReplyTB.REPLYS_IP));
					reply.setReplys_kind(resultSet.getString(ReplyTB.REPLYS_KIND));
					reply.setReplys_name(resultSet.getString(ReplyTB.REPLYS_NAME));
					reply.setReplys_release_id(resultSet.getInt(ReplyTB.REPLYS_RELEASE_ID));
					reply.setReplys_subject(resultSet.getString(ReplyTB.REPLYS_SUBJECT));
					relyList.add(reply);
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
		
		return relyList;
	}
	
	/**
	 * @version 1.0
	 * @param ReleaseTB release
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see insertRelease(ImageTB image)
	 */
	public int getReplyRowNumBySubjectId(int id){
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + ReplyTB.TABLE_NAME + " where " + ReplyTB.REPLYS_RELEASE_ID + "=" + id;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @param String brand
	 * @author Shea
	 * @return ResultSet  
	 * @throws IOException 
	 * @see selectRelatedBrand(String brand,int beginPageNum,int pageSize)
	 */
	public boolean deleteSubject(int id){
		SqlMachine sqlMachine = new SqlMachine(conn);
		return sqlMachine.deleteRow(ReplyTB.TABLE_NAME, ReplyTB.REPLYS_ID, id);
	}
	
	/**
	 * @version 1.0
	 * @param String brand
	 * @author Shea
	 * @return ResultSet  
	 * @throws IOException 
	 * @see 得到最新的回复信息
	 */
	public List<ReplyTB> selectNewReply(int beginPageNum,int pageSize){
		SqlMachine sqlMachine = new SqlMachine(conn);
		return selectReplyByResultSet(sqlMachine.selectAllRecord(ReplyTB.TABLE_NAME,ReplyTB.REPLYS_DATE,beginPageNum,pageSize));
	}
	
}
